import {
  createStore,
  Plugin,
  PluginFunctions,
  PluginOnDocumentLoad,
} from "../../core";

import BookmarkListWithStore from "./BookmarkListWithStore";
import StoreProps from "./StoreProps";
import { ReactElement, useMemo } from "react";

interface BookmarkPlugin extends Plugin {
  Bookmarks: () => ReactElement;
}

const bookmarkPlugin = (): BookmarkPlugin => {
  const store = useMemo(() => createStore<StoreProps>({}), []);

  const BookmarksDecorator = () => <BookmarkListWithStore store={store} />;

  return {
    install: (pluginFunctions: PluginFunctions) => {
      store.update("jumpToDestination", pluginFunctions.jumpToDestination);
    },
    onDocumentLoad: (props: PluginOnDocumentLoad) => {
      store.update("doc", props.doc);
    },
    Bookmarks: BookmarksDecorator,
  };
};

export default bookmarkPlugin;
